package com.example.officialdocumentsys.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;


@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    //自动填充数据库
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        if(ThreadLocalContext.getCurrentId() != null){
            metaObject.setValue("createUser", ThreadLocalContext.getCurrentId());
            metaObject.setValue("updateUser", ThreadLocalContext.getCurrentId());
        }
        metaObject.setValue("updateTime", LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateUser", ThreadLocalContext.getCurrentId());
        metaObject.setValue("updateTime", LocalDateTime.now());
    }
}
